﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using AFT.Parking.Foundation.Extensions;

namespace AFT.Parking.Foundation.Mail
{
    public class Mail : IMail
    {
        Thread thread;

        public bool Send(MailTo mailTo, MailFrom mailFrom)
        {
            this.MailTo = mailTo;
            this.MailFrom = mailFrom;

            thread = new Thread(new ThreadStart(SendMail));
            thread.Start();
            
            return true;
        }

        private void SendMail()
        {
            MailFrom mailFrom = this.MailFrom;
            MailTo mailTo = this.MailTo;
            //邮件发送具体实现方式

            //构建MailFrom
            if (mailFrom.From.IsNullOrEmpty())
            {
                mailFrom = new MailFrom();
                mailFrom.From = "Info@ywcec.com";
                mailFrom.Password = "hcf000000";
                mailFrom.Host = "smtp.exmail.qq.com";
                mailFrom.Port = 25;
                mailFrom.EnableSsl = false;
                mailFrom.DisplayName = "远望手机网";
            }

            //发送邮件
            try
            {
                MailMessage mailMessage = new MailMessage();
                if (!string.IsNullOrEmpty(mailTo.To))
                    mailMessage.To.Add(mailTo.To);//指定收件人
                if (!string.IsNullOrEmpty(mailTo.CC))
                    mailMessage.CC.Add(mailTo.CC);//指定抄送人
                if (!string.IsNullOrEmpty(mailTo.Bcc))
                    mailMessage.Bcc.Add(mailTo.Bcc);//指定密送人
                mailMessage.From = new MailAddress(mailFrom.From, mailFrom.DisplayName, mailFrom.Encoding == null ? Encoding.UTF8 : mailFrom.Encoding);//指定发件人并设置displayName、displayName字符集
                mailMessage.Subject = mailTo.Subject;//设置邮件主题
                mailMessage.SubjectEncoding = mailTo.Encoding == null ? Encoding.UTF8 : mailTo.Encoding;
                mailMessage.Body = mailTo.Body;//设置邮件内容
                mailMessage.BodyEncoding = mailTo.Encoding == null ? Encoding.UTF8 : mailTo.Encoding;
                mailMessage.IsBodyHtml = mailTo.IsBodyHtml;//设置或指示邮件正文是否为html格式
                mailMessage.Priority = MailPriority.Normal;//设置邮件优先级
                if (!string.IsNullOrEmpty(mailTo.ReplyTo))
                {
                    string[] strArr = mailTo.ReplyTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (var obj in strArr)
                    {
                        mailMessage.ReplyToList.Add(new MailAddress(obj));//设置邮件回复地址
                    }
                }

                SmtpClient client = new SmtpClient();
                client.UseDefaultCredentials = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = new NetworkCredential(mailFrom.From, mailFrom.Password);//设置用于验证发件身份的凭据
                client.Host = mailFrom.Host;//设置smtp事务的主机名称或IP地址
                client.Port = mailFrom.Port;//设置smtp事务的端口
                client.EnableSsl = mailFrom.EnableSsl;//是否使用安全套接字层进行连接
                client.Timeout = 1000 * 60;
                try
                {
                    client.Send(mailMessage);
                }
                catch (Exception ex)
                {
                    //写日志
                    string strMessage = ex.Message;
                }
            }
            catch
            {
                //写日志
            }
            finally
            {
                thread.Abort();
            }
        }

        private MailTo MailTo
        {
            get;
            set;
        }

        public MailFrom MailFrom
        {
            get;
            set;
        }
    }
}
